<?php

namespace app\admin\model;

use think\Db;
use think\Model;

class AuthruleModel extends Model
{

    // 设置数据表（不含前缀）
    protected $name = 'auth_rule';

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->_collection = Db::name($this->name);
    }


    public function authRuleTree()
    {
        $result = $this->_collection->order('sort desc')->select();
        return $this->sort($result);
    }

    public function sort($data, $pid = 0)
    {
        static $arr = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $arr[] = $v;
                $this->sort($data, $v['id']);
            }
        }
        return $arr;
    }


    public function getAuthByid($authid)
    {
        $result = $this->_collection->where('id', 'in', $authid)->where('style', 0)->select();
        return $result;
    }

    public function  getadminauth(){
        $result = $this->_collection->where('style', 0)->select();
        return $result;
    }


    /**
     * @param $a
     * @return $pid
     * @param 递归
     */
     public function get_attr($query,$pid){
        $tree = array();
        foreach($query as $v){
            if($v['pid'] == $pid){
                $v['children'] = $this->get_attr($query,$v['id']);
                if($v['children'] == null){
                    $v['children'] = '';
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }

    /**
     * @param 获取权限
     */
    public function Auths(){
        $query = Db::name($this->name)->field('id,title,pid,name,level,navlevel')->select();
        $list = $this->get_attr($query,0);
//        $query = Db::name($this->name)->select();
        return $list;
    }


    /**
     * @param $where
     * @param $page
     * @param $limit
     * @param 权限列表
     */
    public function listction($where,$page,$limit){
        $query = Db::name($this->name)->where($where)->page($page)->limit($limit)->order('id desc')->select();
        return $query;
    }


    /**
     * @param 获取权限节点
     */
    public function authlist($where){
        $query = Db::name($this->name)->where($where)->field('id,title,name,style')->select();
        return $query;
    }


    /**
     * @param $funname
     * @param 验证方法名唯一
     */
    public function functionname($where){
        $query = Db::name($this->name)->where($where)->find();
        return $query;
    }


    /**
     * @param $data
     * @param 添加权限
     */
    public function adddata($data){
        // 启动事务
        Db::startTrans();
        try{
            $query = Db::name($this->name)->insert($data);
            // 提交事务
            Db::commit();
            return $query;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * @param $id
     * @param 删除权限
     */
    public function deldata($id){
        // 启动事务
        Db::startTrans();
        try{
            $query = Db::name($this->name)->where('id',$id)->delete();
            // 提交事务
            Db::commit();
            return $query;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * @param $id
     * @param $data
     * @param 修改权限
     */
    public function editdata($id,$data){
        // 启动事务
        Db::startTrans();
        try{
            $query = Db::name($this->name)->where('id',$id)->update($data);
            // 提交事务
            Db::commit();
            return $query;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    public function county($where){
        $ruslt = Db::name($this->name)->where($where)->count();
        return $ruslt;
    }


    public function titles($where){
        $query = Db::name($this->name)->where($where)->field('title')->select();
        return $query;
    }

}